<template>
  <section class="ProjectDemandIndex-container">
    <project-demand-search
      ref="search"
      @onSearch="doSearch"
      @onModule="doModule"
      @onProject="doProject"
    >
      <project-demand-list :needss="needss" :project="project" :module="module">
        <template slot="action-bar">
          <quick-action-bar>
            <quick-icon-button
              icon="icon-icon_xinzeng"
              tooltip="新增"
              @click="showAddDialog(module, project)"
            />
            <quick-icon-button
              icon="icon-daochu"
              tooltip="返回"
              @click="returnModule"
            />
          </quick-action-bar>
        </template>
        <template slot="row-action-bar" slot-scope="scope">
          <quick-action-bar>
            <quick-icon-button
              icon="icon-xiugai"
              tooltip="编辑"
              @click="showEditDialog(scope.row, module, project)"
            />
            <quick-icon-button
              icon="icon-shanchu"
              tooltip="删除"
              @click="deleteneeds(scope.row.id)"
            />
            <quick-icon-button
              icon="icon-xiangqing-copy"
              tooltip="详情"
              @click="detailneeds(scope.row)"
            />
          </quick-action-bar>
        </template>
      </project-demand-list>
    </project-demand-search>

    <project-demand-add ref="addDialog" @added="refresh" />
    <project-demand-edit ref="editDialog" @edited="refresh" />
  </section>
</template>

<script>
import ProjectDemandSearch from "./ProjectDemandSearch";
import ProjectDemandList from "./ProjectDemandList";
import ProjectDemandAdd from "./ProjectDemandAdd.vue";
import ProjectDemandEdit from "./ProjectDemandEdit.vue";
import _api from "../../common/api/project_demand";

export default {
  components: {
    ProjectDemandSearch,
    ProjectDemandList,
    ProjectDemandAdd,
    ProjectDemandEdit,
  },
  data() {
    return {
      needss: [],
      module: [],
      project: [],
    };
  },
  methods: {
    doSearch(needss) {
      this.needss = needss;
    },

    doModule(module) {
      this.module = module;
    },

    doProject(project) {
      this.project = project;
    },

    showAddDialog(module, project) {
      this.$refs.addDialog.showDialog(module, project);
    },

    showEditDialog(needs, module, project) {
      this.$refs.editDialog.showDialog(needs, module, project);
    },

    showSetRolesDialog(needs_id) {
      this.$refs.setRolesDialog.showDialog(needs_id);
    },

    deleteneeds(id) {
      console.log(id);
      this.$confirm("你确定删除此项吗？", "确认删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          try {
            await _api.deleteAsync(id);
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.refresh();
          } catch (error) {
            this.$message({
              type: "info",
              message: `删除失败，${error}`,
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    refresh() {
      this.$refs.search.getProjectDemand();
    },

    // 返回到module
    returnModule() {
      this.$router.push({
        name: "ProjectModuleList",
      });
    },

    detailneeds(scope) {
      this.$router.push({
        name: "ProjectTaskList",
        params: {
          needs: scope,
        },
      });
    },
  },
  mounted() {},
};
</script>

<style scoped lang="scss">
.ProjectDemandIndex-container {
  height: 100%;
}
</style>